package com.huiyujiaoyu.powertime.ui.my;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.huiyujiaoyu.base.BaseActivity;
import com.huiyujiaoyu.http.HttpMethods;
import com.huiyujiaoyu.powertime.MainActivity;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.LoginBean;
import com.huiyujiaoyu.powertime.model.UserInfoBean;
import com.huiyujiaoyu.powertime.ui.login.ImproveInfoActivity;
import com.huiyujiaoyu.util.ActivityUtil;
import com.huiyujiaoyu.util.RegexUtils;
import com.huiyujiaoyu.util.TimeDownTool;
import com.huiyujiaoyu.util.ToastUtil;
import com.huiyujiaoyu.widget.LlkjToolBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.R.attr.x;
import static com.huiyujiaoyu.http.HttpApi.HTTP_GETVERIFICATIONCODE;
import static com.huiyujiaoyu.http.HttpApi.HTTP_LOGIN;
import static com.huiyujiaoyu.http.HttpApi.HTTP_YZYZM;

/**
 * Created by win7 on 2017/6/18.
 * 修改手机号 发送验证界面
 */

public class UpdataPhoneVerifActivity extends BaseActivity implements TimeDownTool.OnTimeDownListener {

    @BindView(R.id.toolbar)
    LlkjToolBar mToolbar;

    public static void launch(Context context) {
        ActivityUtil.startActivity(context, UpdataPhoneVerifActivity.class);
    }


    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_send)
    Button btnSend;
    @BindView(R.id.btn_save)
    Button btnSave;
    @BindView(R.id.tv_lianxi_kefu)
    TextView tvLianxiKefu;
    private String code;
    private String mobile;
    TimeDownTool timer;


    public void initView() {
        setToolbar(mToolbar);
        mToolbar.setToolBar("修改手机号", true, "",R.mipmap.icon_titlebar_back, false, "", LlkjToolBar.isNull);
        timer = new TimeDownTool(60000, 1000);
        timer.setOnTimeDownListener(UpdataPhoneVerifActivity.this);
        etPhone.setText("当前手机号:"+UserInfoBean.getInstance().getPhone());
        etPhone.setEnabled(false);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_phone_verif;
    }


    /**
     * 发送验证码
     */
    private void sendCode() {
        mobile = UserInfoBean.getInstance().getPhone();
        if (TextUtils.isEmpty(mobile)) {
            ToastUtil.makeShortText(mContext,"请输入手机号");
            return;
        }
        if (!RegexUtils.isMobilePhoneNumber(mobile)) {
            ToastUtil.makeShortText(mContext,"请输入正确的手机号");
            return;
        }
        timer.start();
        HttpMethods.getInstance().getVerificationCode(mContext,getComp(),this,mobile,1);

    }


    @OnClick({R.id.btn_send, R.id.btn_save, R.id.tv_lianxi_kefu})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_send:
                sendCode();
                break;
            case R.id.btn_save:
                code = etCode.getText()+"";
                mobile = UserInfoBean.getInstance().getPhone();
                if(!TextUtils.isEmpty(code)){
                    HttpMethods.getInstance().yzyzm(mContext,getComp(),this,mobile,code,2);
                }else{
                    ToastUtil.makeShortText(mContext,"请输入验证码");
                }
                break;
            case R.id.tv_lianxi_kefu:
                break;
        }
    }

    @Override
    public void onFinish() {
        btnSend.setEnabled(true);
        btnSend.setText("获取验证码");
    }

    @Override
    public void onTick(long millisUntilFinished) {
        btnSend.setEnabled(false);
        btnSend.setText((millisUntilFinished / 1000) + "s");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }

    }
    @Override
    public void onNext(Object o, int httpcode) {
        super.onNext(o, httpcode);
        switch (httpcode) {
            case HTTP_GETVERIFICATIONCODE:
                ToastUtil.makeShortText(mContext,"已发送验证码");
                break;
            case HTTP_YZYZM:
                UpdataPhoneActivity.launch(mContext);
                break;
        }
    }
}
